package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.DownloadHistory;
import com.tbit.uqbike.object.pojo.vo.DownloadHistoryVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.base.TokenService;
import com.tbit.uqbike.service.business.DownloadHistoryService;
import com.tbit.uqbike.webmanager.dao.capital.DownloadHistoryDao;
import com.tbit.uqbike.webmanager.util.csv.config.CsvExportConfig;
import com.tbit.uqbike.webmanager.util.csv.entity.RecordDownloadDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class DownloadHistoryServiceImpl implements DownloadHistoryService {

    @Autowired
    DownloadHistoryDao dao;
    @Autowired
    private TokenService tokenService;

    @Autowired
    CsvExportConfig config;

    @Override
    public PageResult<DownloadHistoryVO> page(String operatorPhone, String operatorName, Date startTime, Date endTime, Integer pageNO, Integer rowCount) {

        LoginInfo loginInfo = tokenService.getLoginInfo();

        DownloadHistory history = new DownloadHistory();
        history.setOperatorName(operatorName);
        history.setOperatorPhone(operatorPhone);
        history.setDelFlag(0);
        //不是管理员，只能查询自己的信息
        if (!loginInfo.roleIsAdmin()) {
            history.setOperatorName(loginInfo.getName());
            history.setOperatorPhone(loginInfo.getPhone());
        }
        PageHelper.startPage(pageNO, rowCount);
        QueryWrapper<DownloadHistory> wrapper = Wrappers.query(history)
                .orderByDesc("createTime");
        List<DownloadHistory> historyList = dao.selectList(wrapper);
        PageInfo<DownloadHistoryVO> pageInfo = new PageInfo(historyList);

        List<DownloadHistoryVO> voList = historyList.stream().map(v -> {
            int expire = System.currentTimeMillis() > v.getExpireTime().getTime() ? 1 : 0;
            DownloadHistoryVO historyVO = new DownloadHistoryVO();
            historyVO.setDownloadId(v.getDownloadId().toString());
            historyVO.setFileName(v.getFileName());
            historyVO.setFileUrl(v.getFileUrl());
            historyVO.setOperatorId(v.getOperatorId());
            historyVO.setOperatorName(v.getOperatorName());
            historyVO.setOperatorPhone(v.getOperatorPhone());
            historyVO.setStatus(v.getStatus());
            historyVO.setDelFlag(v.getDelFlag());
            historyVO.setCreateTime(v.getCreateTime());
            historyVO.setUpdateTime(v.getUpdateTime());
            historyVO.setExpireTime(v.getExpireTime());
            historyVO.setExpire(expire);
            return historyVO;
        }).collect(Collectors.toList());
        return new PageResult<>(pageNO, rowCount, pageInfo.getTotal(), pageInfo.getPages(), voList);
    }

    @Override
    public void del(Long id) {
        DownloadHistory downloadHistory = dao.selectById(id);
        downloadHistory.setDelFlag(1);
        dao.updateById(downloadHistory);
    }

    @Override
    public int recordSuccess(RecordDownloadDTO downloadDTO) {
        DownloadHistory downloadHistory = new DownloadHistory();
        downloadHistory.setFileUrl(downloadDTO.getUrl());
        downloadHistory.setStatus(2);
        downloadHistory.setDelFlag(0);
        downloadHistory.setCreateTime(new Date());
        downloadHistory.setUpdateTime(new Date());
        downloadHistory.setFileName(downloadDTO.getFileName());
        LoginInfo loginInfo = tokenService.getLoginInfo();
        downloadHistory.setOperatorId(loginInfo.getAccountUserId());
        downloadHistory.setOperatorName(loginInfo.getName());
        downloadHistory.setOperatorPhone(loginInfo.getPhone());
        if (downloadDTO.getExpire() != null) {
            downloadHistory.setExpireTime(new Date(System.currentTimeMillis() + downloadDTO.getExpire()));
        } else {
            downloadHistory.setExpireTime(new Date(System.currentTimeMillis() + config.getOssExpire()));
        }
        return dao.insert(downloadHistory);

    }
}
